/* 
 * Cognitive Profile Copyright (C) 2007 Lois Breur Krause
 */
package cpi;

import org.syntelos.lang.OutputStream;
import org.syntelos.lang.Type;
import org.syntelos.sys.Reference;
import org.syntelos.sx.Request;
import org.syntelos.sx.Response;

/**
 * 
 */
public class Post
    extends org.syntelos.sx.methods.Post
{

    public static class Responses
        extends java.lang.Object
        implements org.w3c.dom.traversal.NodeFilter
    {
        public Responses(){
            super();
        }
        public short acceptNode(org.w3c.dom.Node node){
            if (org.w3c.dom.Node.ELEMENT_NODE == node.getNodeType()){
                String ln = node.getLocalName();
                if ("response".equals(ln))
                    return FILTER_ACCEPT;
                else if ("result".equals(ln))
                    return FILTER_REJECT;
                else
                    return FILTER_SKIP;
            }
            else
                return FILTER_REJECT;
        }
    }
    public final static Responses RESPONSES = new Responses();


    public Post(){
        super();
    }


    @Override
    public void respond(Request req)
        throws java.io.IOException
    {
        this.setHeader(Version.Instance);
        try {
            org.w3c.dom.Document doc = req.readDocument();
            doc = this.product(doc);
            this.setContentTypeXml();
            this.writeDocument(doc);
        }
        catch (org.w3c.dom.DOMException exc){
            this.setStatusError();
        }
    }

    protected final org.w3c.dom.Document product(org.w3c.dom.Document doc)
        throws org.w3c.dom.DOMException
    {
        float raw_sf = 0f;
        float raw_st = 0f;
        float raw_nf = 0f;
        float raw_nt = 0f;

        float raw_max = 0f;

        float normalized_sf = 0f;
        float normalized_st = 0f;
        float normalized_nf = 0f;
        float normalized_nt = 0f;

        org.w3c.dom.traversal.NodeIterator responses =
            this.createNodeIterator(doc,RESPONSES);

        org.w3c.dom.Element next;
        int cc = -1;
        while (null != (next = (org.w3c.dom.Element)responses.nextNode())){
            cc += 1;
            java.lang.Integer usr_r = Tools.ValueOfInteger(next);
            if (null == usr_r)
                throw new java.lang.IllegalArgumentException("Incomplete CPI response.");
            else {
                int usr_ri = usr_r.intValue();
                Group.Type usr_grp = null;
                float usr_val;
                switch (usr_ri){
                case 0:
                    usr_grp = Key.TypeOf(true,cc);
                    usr_val = 4;
                    break;
                case 1:
                    usr_grp = Key.TypeOf(true,cc);
                    usr_val = 3;
                    break;
                case 2:
                    usr_grp = Key.TypeOf(true,cc);
                    usr_val = 2;
                    break;
                case 3:
                    usr_grp = Key.TypeOf(true,cc);
                    usr_val = 1;
                    break;
                case 4:
                    usr_grp = Key.TypeOf(false,cc);
                    usr_val = 1;
                    break;
                case 5:
                    usr_grp = Key.TypeOf(false,cc);
                    usr_val = 2;
                    break;
                case 6:
                    usr_grp = Key.TypeOf(false,cc);
                    usr_val = 3;
                    break;
                case 7:
                    usr_grp = Key.TypeOf(false,cc);
                    usr_val = 4;
                    break;
                default:
                    throw new java.lang.IllegalArgumentException("Invalid CPI response.");
                }
                if (Group.Type.SF == usr_grp){
                    raw_sf += usr_val;
                    if (raw_sf > raw_max)
                        raw_max = raw_sf;
                }
                else if (Group.Type.ST == usr_grp){
                    raw_st += usr_val;
                    if (raw_st > raw_max)
                        raw_max = raw_st;
                }
                else if (Group.Type.NF == usr_grp){
                    raw_nf += usr_val;
                    if (raw_nf > raw_max)
                        raw_max = raw_nf;
                }
                else if (Group.Type.NT == usr_grp){
                    raw_nt += usr_val;
                    if (raw_nt > raw_max)
                        raw_max = raw_nt;
                }
                else
                    throw new java.lang.IllegalStateException("bug");
            }
        }

        normalized_sf = (raw_sf/raw_max);
        normalized_st = (raw_st/raw_max);
        normalized_nt = (raw_nt/raw_max);
        normalized_nf = (raw_nf/raw_max);

        org.w3c.dom.Element result = Tools.GetElementByName(doc,"result");
        if (null != result){
            org.w3c.dom.Element st = Tools.GetElementByName(result,"st");
            Tools.SetChildValueText(st,"raw",raw_st);
            Tools.SetChildValueText(st,"scaled",normalized_st);

            org.w3c.dom.Element sf = Tools.GetElementByName(result,"sf");
            Tools.SetChildValueText(sf,"raw",raw_sf);
            Tools.SetChildValueText(sf,"scaled",normalized_sf);

            org.w3c.dom.Element nt = Tools.GetElementByName(result,"nt");
            Tools.SetChildValueText(nt,"raw",raw_nt);
            Tools.SetChildValueText(nt,"scaled",normalized_nt);

            org.w3c.dom.Element nf = Tools.GetElementByName(result,"nf");
            Tools.SetChildValueText(nf,"raw",raw_nf);
            Tools.SetChildValueText(nf,"scaled",normalized_nf);

            java.util.Date timestamp = new java.util.Date();
            Tools.SetChildValueText(result,"date",timestamp);

            return doc;
        }
        else
            throw new java.lang.IllegalArgumentException("Invalid CPI request document.");
    }
}
